中文
Paddle.js Core
As the core part of the Paddle.js ecosystem, this package hosts @paddlejs/paddlejs-core
,
which is responsible for the operation of the inference process of the entire engine,
and provides interfaces for backend registration and environment variable registration.
RunnerConfig
When registering the engine you need to configure the engine, you must configure the items modelPath
, feedShape
, all items are configured as follows.
enum GraphType {
SingleOutput = 'single',
MultipleOutput = 'multiple',
MultipleInput = 'multipleInput'
}
interface RunnerConfig {
modelPath: string;
modelName?: string;
feedShape: {
fc?: number;
fw: number;
fh: number;
};
fill?: Color;
mean?: number[];
std?: number[];
bgr?: boolean;
type?: GraphType;
needPreheat?: boolean;
plugins?: {
preTransforms?: Transformer[];
transforms?: Transformer[];
postTransforms?: Transformer[];
};
}
Importing
You can install this package via npm., @paddlejs/paddlejs-core
import { Runner } from '@paddlejs/paddlejs-core';
import '@paddlejs/paddlejs-backend-webgl';
const runner = new Runner({
modelPath: '/model/mobilenetv2',
feedShape: {
fw: 256,
fh: 256
},
fill?: '#fff',
webglFeedProcess?: true
});
await runner.init();
const res = await runner.predict(mediadata, callback?);
Note: If you are importing the Core package, you also need to import a backend (e.g.,
paddlejs-backend-webgl, paddlejs-backend-webgpu).
High-level use
-
@paddlejs/paddlejs-core
provides the interface registerOp
, through which developers can register custom operators.
-
@paddlejs/paddlejs-core
provides the global variable env
module, through which developers can register environment variables, using the following method:
env.set(key, value);
env.get(key);
-
transform model stucture
By registering the model transformers through runnerConfig.plugins
, developers can make changes (add, delete, change) to the model structure, such as pruning to remove unnecessary layers to speed up inference, or adding custom layers to the end of the model and turning post-processing into layers in the model to speed up post-processing.
-
Turn on performance flag for acceleration
Paddle.js currently provides five performance flags
, which can be set to true
if you want to enable inference acceleration.
env.set('webgl_pack_channel', true);
Turn on webgl_pack_channel
and the eligible conv2d will use packing shader to perform packing transformations to improve performance through vectorization calculations.
env.set('webgl_force_half_float_texture', true);
Enable webgl_force_half_float_texture
, feature map uses half float HALF_FLOAT
.
env.set('webgl_gpu_pipeline', true);
Turn on webgl_gpu_pipeline
to convert all model pre-processing parts to shader processing, and render the model results to WebGL2RenderingContext
of webgl backend on screen. Developers can perform model post-processing on the output texture and the original image texture to achieve the GPU_PIPELINE
: pre-processing + inference + post-processing (rendering processing) to get high performance. Take humanseg model case for reference.
env.set('webgl_pack_output', true);
Enable webgl_pack_output
to migrate the NHWC
to NCHW
layout transformation of the model output to the GPU, and pack
to a four-channel layout to reduce loop processing when reading the results from the GPU